package com.quanxi.qxaimain.model.indicators;

import com.alibaba.fastjson.JSONObject;
import com.quanxi.qxaimain.common.globalException.ResultCode;
import com.quanxi.qxaimain.common.util.sqlFormat.sqlFormat;
import com.quanxi.qxaimain.dao.indicators.indicatorsDataMapper;
import com.quanxi.qxaimain.dao.produce.produceMapper;
import com.quanxi.qxaimain.service.hystrixServer.hystrixServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author lyx
 * @create 2022/9/5 16:53
 */
@Slf4j
@Service("indicatorsGet")
public class indicatorsGetImpl implements indicatorsGet {
   @Resource
   indicatorsDataMapper indicatorsDataMapper;
   @Resource
    produceMapper produceMapper;
   @Resource
    hystrixServer hystrixServer;
    @Override
    public JSONObject getSqlresult(String sql, String D1, String D2, String dept_code) {
        JSONObject jsonObject = new JSONObject();
        String sqlformat= sqlFormat.sqlFormat(sql,D1,D2,dept_code);
         Long starttime = System.currentTimeMillis();

      if ("".equals(sqlformat)){
          jsonObject.put("sql_message", ResultCode.SQL_NUll);
          jsonObject.put("sql_result","");
          jsonObject.put("sql",sqlformat);
          jsonObject.put("time", System.currentTimeMillis()-starttime);
          return jsonObject;
      }
         jsonObject = hystrixServer.exec_sql_overtime_no(sqlformat);
       // jsonObject = hystrixServer.exec_sql_overtime_60s(sqlformat);
      //  jsonObject=   hystrixServer.exec_sql_overtime_mondb(sqlformat);
        return jsonObject;
    }

    @Override
    public JSONObject getProduceresult(  Map<String, Object> map ) {

        JSONObject jsonObject=new JSONObject();
        String result="";
        String message="";
        long starTime = System.currentTimeMillis();
        try{
            produceMapper.produce(map);
            if ((Integer) map.get("ErrCode")==0){
//                List<LinkedHashMap<String, Object>> o=  (List<LinkedHashMap<String, Object>>)map.get("p_cur");
//                if (o.size()==0){
//                    result=null;
//                }
//                else {
//                    result= JSON.toJSONString(o, SerializerFeature.WriteMapNullValue);
//                }
                message = "存储过程成功";
            }

            if ((Integer) map.get("ErrCode")==1){
                message="存储过程失败:"+ map.get("ErrStr").toString();
            }

        }catch (Exception e){
            String error= e.getMessage();
            message="存储过程失败:"+error;

        }


        long  endTime=System.currentTimeMillis();
        jsonObject.put("result",map);//
        jsonObject.put("message",message);//
        jsonObject.put("time", (endTime-starTime)+"ms");//执行时间);

        return jsonObject;
    }


}